const crypto = require('crypto');

function aesDecrypt(msgSecret, appSecret) {
//   const msgSecret = "qa3eqTSSX4VTr2q50u71505aG1pQteTgNawlVpFus+DOsGn+nzODxmoeqYRHfqRKrLLueuUVDuehR0r4I5PNqH4Nba140XUHVrLPjX7qKvMxat7JNfvFsMvqKpeRkC2jWZ9NLKv/wBYAhAFKkWCl4yNmc7odKDb4mNgrExrSBQvXMdd40ELKbMU0HcCxXIi1ukm9mSvpGelxM7Vs6YAqPyPzn9RRJuoXZZjpHRIzZpkpGsa0PMnjcYIC3hjz3AbKaW+EIAAYWq85ozKkTnbT7D7VQOF+ivk57Y0M3Qe1kQWQiCkifJK52YaDiprv7tChp37bEiwzd91FfclUPa2GgkwBCh0DaiJAPWn9E5U3byCZO0UUPlUPDLZFp3WWRGmMANtN4kBS7uGaOMs3WJGREGe471CXFVuKCYkepULdmQM+POecOQSQR4U76eXJKYVhSSdAsypZM7Wev7PyyaODpj7ecECIBFDES7k3Nh9GAJDooiziXQKGBQXOrjxfWk6Pi4IFxa6+4fQsbl93XtdOpvkkxbg5ZqcX1d2E01lp/F0VQm0eerRw/fKG9+d7NmsAWkDIarKtyDXrPRLIrWlzVNhGmlYJ+CfPpqKtI4md7zhhnO/XN1LKCMz/XEM7uwP/ ";
//   const appSecret = "a5c5a2dc-31ca-4a7a-b743-e862d96082bd ";
  const key = appSecret.replace(/-/g, '').substring(0, 32); // 使用前32个字符作为密钥
  const decrypt = decryptAes(msgSecret, key);
  console.log("解密之后的数据为: " + decrypt);
  return decrypt
}

function decryptAes(sSrc, sKey) {
   
  try {
    const key = Buffer.from(sKey, 'utf8');
    const iv = Buffer.from(sKey.substring(0, 16), 'utf8');
    const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
    let decrypted = decipher.update(sSrc, 'base64', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted;
  } catch (ex) {
    return null;
  }
}

export default aesDecrypt;
